import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:spring_fairy/model/config.dart';

class ThemeColorPage extends StatefulWidget {
  const ThemeColorPage({super.key});

  @override
  State<ThemeColorPage> createState() => _ThemeColorPageState();
}

class _ThemeColorPageState extends State<ThemeColorPage> {
  Color currentColor = AppConfig.PRIMARY_COLOR;

  void changeColor(Color color) {
    setState(() {
      currentColor = color;
      AppConfig.PRIMARY_COLOR = color;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("选择主题颜色"),
        backgroundColor: currentColor,
      ),
      body: Column(
        children: [
          const SizedBox(height: 20),
          Text("当前选择颜色", style: Theme.of(context).textTheme.titleLarge),
          const SizedBox(height: 10),
          CircleAvatar(
            backgroundColor: currentColor,
            radius: 30,
          ),
          const SizedBox(height: 20),
          // 调色板
          ColorPicker(
            pickerColor: currentColor,
            onColorChanged: changeColor,
            enableAlpha: false,
            displayThumbColor: true,
            labelTypes: const [ColorLabelType.hex],
            pickerAreaHeightPercent: 0.7,
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            style: ElevatedButton.styleFrom(backgroundColor: currentColor),
            onPressed: () {
              // TODO: 保存颜色或应用主题
              setState(() {
                AppConfig.PRIMARY_COLOR = currentColor;
              });
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text("颜色已保存（模拟）")),
              );

              Navigator.pop(context, currentColor);
            },
            child: const Text("保存"),
          ),
        ],
      ),
    );
  }
}
